package cn.zn.controller.job;

import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.job.service.SysJobService;
import cn.zn.job.vo.SysJobEditVo;
import cn.zn.job.vo.SysJobPageVo;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 调度任务信息操作处理
 *
 * @author zn-admin
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController {

    @Autowired
    private SysJobService sysJobService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public ResultData list(SysJobPageVo vo) {
        Object result = sysJobService.selectJobList(vo);
        return ResultUtil.success(result);
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:info')")
    @GetMapping(value = "/{jobId}")
    public ResultData getInfo(@PathVariable("jobId") String jobId) {
        return ResultUtil.success(sysJobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody SysJobEditVo vo) throws Exception {
        sysJobService.insertJob(vo);
        return ResultUtil.success();
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{jobId}")
    public ResultData edit(@PathVariable("jobId") String jobId, @RequestBody SysJobEditVo vo) throws SchedulerException {
        sysJobService.updateJob(jobId, vo);
        return ResultUtil.success();
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{jobId}")
    public ResultData changeStatus(@PathVariable("jobId") String jobId, @RequestBody SysJobEditVo vo) throws SchedulerException {
        sysJobService.changeStatus(jobId, vo);
        return ResultUtil.success();
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:execute')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run/{jobId}")
    public ResultData run(@PathVariable("jobId") String jobId) throws SchedulerException {
        sysJobService.run(jobId);
        return ResultUtil.success();
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public ResultData remove(@PathVariable String[] jobIds) throws SchedulerException {
        sysJobService.deleteJob(jobIds);
        return ResultUtil.success();
    }
}
